﻿using QLKetNoi;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Linq;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QLDVu.GUI
{
    public partial class QuanLyPhieuThuForm : Form
    {
        string phieuthutien;
        bool kiemtrathem = true;
        public static string inmaphieuthu;
        public static string NextID(string lastID, string prefixID)
        {
            int nextID = int.Parse(lastID.Remove(0, prefixID.Length)) + 1;
            int lengthNumerID = lastID.Length - prefixID.Length;
            string zeroNumber = "";
            for (int i = 1; i <= lengthNumerID; i++)
            {
                if (nextID < Math.Pow(10, i))
                {
                    for (int j = 1; j <= lengthNumerID - i; i++)
                    {
                        zeroNumber += "0";
                    }
                    return prefixID + zeroNumber + nextID.ToString();
                }
            }
            return prefixID + nextID;
        }
        public QuanLyPhieuThuForm()
        {
            InitializeComponent();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            kiemtrathem = false;
            cbNhanVien.Enabled = true;
            txtNguoiNopTien.ReadOnly = false;
            txtDienThoai.ReadOnly = false;
            txtDiaChi.ReadOnly = false;
            txtSoTienNop.ReadOnly = false;
            
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (kiemtrathem == false)
            {
                if (txtNguoiNopTien.Text.Trim() == "")
                {
                    MessageBox.Show("Người nộp tiền không được để trống");

                    txtNguoiNopTien.Focus();
                    return;
                }
                if (txtDienThoai.Text.Trim() == "")
                {
                    MessageBox.Show("Điện thoại không được để trống");

                    txtDienThoai.Focus();
                    return;
                }
                if (txtSoTienNop.Text.Trim() == "")
                {
                    MessageBox.Show("Số tiền nộp không được để trống");

                    txtSoTienNop.Focus();
                    return;
                }
                if (txtDiaChi.Text.Trim() == "")
                {
                    MessageBox.Show("Địa chỉ không được để trống");

                    txtDiaChi.Focus();
                    return;
                }
                BO.PhieuThuTienBO.CapNhatPhieuThuTien(txtPhieuThuTien.Text, cbNhanVien.SelectedValue.ToString(),
                    txtNguoiNopTien.Text, cbPhieuSuaChua.SelectedValue.ToString(), dpNgayNop.Value.ToShortDateString(),
                    txtSoTienNop.Text, txtConLai.Text, txtDiaChi.Text, txtDienThoai.Text);

               

                Table<PHIEUTHU> danhsachphieuthu = BO.PhieuThuTienBO.DanhSachPhieuThu();
                var dsphieubaogia = from ds in danhsachphieuthu
                                    select new { ds.MaPhieuThuTien, ds.MaPhieuSuaChua, ds.NguoiNopTien, ds.DienThoai, ds.TienThu, ds.TienConLai };
                gvPhieuThuTien.DataSource = dsphieubaogia;
                LoadPhieuThuTien(phieuthutien);
                MessageBox.Show("Đã cập nhật phiếu thu thành công");
            }
            else
            {
                bool kt = false;
                if (txtNguoiNopTien.Text.Trim() == "")
                {
                    MessageBox.Show("Bạn phải nhập người nộp tiền");
                    kt = true;
                    return;
                }
                if (txtSoTienNop.Text.Trim() == "")
                {
                    MessageBox.Show("Bạn phải nhập số tiền nộp");
                    kt = true;
                    return;
                }
                if (txtDienThoai.Text.Trim() == "")
                {
                    MessageBox.Show("Bạn phải nhập số điện thoại");
                    kt = true;
                    return;
                }
                if (txtDiaChi.Text.Trim() == "")
                {
                    MessageBox.Show("Bạn phải nhập địa chỉ");
                    kt = true;
                    return;
                }
                if (Convert.ToInt32(txtConLai.Text) < 0)
                {
                    MessageBox.Show("Số tiền còn lại không được nhỏ hơn 0");
                    kt = true;
                    return;
                }
                if (kt == false)
                {
                    string maphieuthu = "";
                    if (BO.PhieuThuTienBO.MaPhieuThuCuoiCung() != null)
                        maphieuthu = NextID(BO.PhieuThuTienBO.MaPhieuThuCuoiCung().MaPhieuThuTien, "PT");
                    else maphieuthu = "PT0001";
                    BO.PhieuThuTienBO.ThemPhieuThuTien(maphieuthu, cbNhanVien.SelectedValue.ToString(), txtNguoiNopTien.Text.Trim(), cbPhieuSuaChua.SelectedValue.ToString(), dpNgayNop.Value.ToShortDateString(), txtSoTienNop.Text.Trim(), txtConLai.Text.Trim(), txtDienThoai.Text.Trim(), txtDiaChi.Text.Trim());
                    BO.PhieuThuTienBO.CapNhatPhieuSuaChua(cbPhieuSuaChua.SelectedValue.ToString(), txtConLai.Text.Trim());
                    txtNguoiNopTien.Text = "";
                    txtSoTienNop.Text = "0";
                    txtDiaChi.Text = "";
                    txtDienThoai.Text = "";
                    SUACHUA ps = BO.PhieuThuTienBO.PhieuSuaChuaTheoMa(cbPhieuSuaChua.SelectedValue.ToString());
                    BAOGIA bg = BO.PhieuThuTienBO.PhieuBaoGiaTheoMa(ps.MaBaoGia);
                    txtTongTien.Text = Convert.ToInt32(bg.TongTien).ToString();
                    txtConLai.Text = Convert.ToInt32(ps.ConLai).ToString();
                    Table<PHIEUTHU> danhsachphieuthu = BO.PhieuThuTienBO.DanhSachPhieuThu();
                    var dsphieubaogia = from ds in danhsachphieuthu
                                        select new { ds.MaPhieuThuTien, ds.MaPhieuSuaChua, ds.NguoiNopTien, ds.DienThoai, ds.TienThu, ds.TienConLai };
                    gvPhieuThuTien.DataSource = dsphieubaogia;
                    LoadPhieuThuTien(phieuthutien);
                    MessageBox.Show("Bạn đã thềm thành công");
                }
            }
            cbPhieuSuaChua.Enabled = false;
            cbNhanVien.Enabled = false;
            txtNguoiNopTien.ReadOnly = true;
            txtDienThoai.ReadOnly = true;
            txtDiaChi.ReadOnly = true;
            txtSoTienNop.ReadOnly = true;
            btnThem.Enabled = true;
            btnSua.Enabled = true;
            btnLuu.Enabled = false;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DialogResult traloi = MessageBox.Show("Bạn có muốn xóa không");
            if (traloi == DialogResult.OK)
            {
                BO.PhieuThuTienBO.XoaPhieuThuTien(txtPhieuThuTien.Text.Trim());
             
                MessageBox.Show("Bạn đã xóa thành công");
            }
            cbPhieuSuaChua.Enabled = false;
            cbNhanVien.Enabled = false;
            txtNguoiNopTien.ReadOnly = true;
            txtDienThoai.ReadOnly = true;
            txtDiaChi.ReadOnly = true;
            txtSoTienNop.ReadOnly = true;
           
            Table<PHIEUTHU> danhsachphieuthu = BO.PhieuThuTienBO.DanhSachPhieuThu();
            var dsphieubaogia = from ds in danhsachphieuthu
                                select new { ds.MaPhieuThuTien, ds.MaPhieuSuaChua, ds.NguoiNopTien, ds.DienThoai, ds.TienThu, ds.TienConLai };
            gvPhieuThuTien.DataSource = dsphieubaogia;
            LoadPhieuThuTien(phieuthutien);
        }

        private void QuanLyPhieuThuForm_Load(object sender, EventArgs e)
        {
            try
            {
                var ds = from psc in BO.PhieuThuTienBO.DanhSachPhieuSuaChua()
                         select new { psc.MaSuaChua, psc.BAOGIA.KHACHHANG.TenKhachHang, psc.BAOGIA.XE.BienSoXe };
                cbPhieuSuaChua.DataSource = ds;
                cbPhieuSuaChua.DisplayMember = "MaSuaChua";
                cbPhieuSuaChua.ValueMember = "MaSuaChua";
                //cbPhieuSuaChua.DropDownColumns = "MaSuaChua,TenKhachHang,BienSoXe";
                //cbPhieuSuaChua.DropDownColumnsHeaders = "Phiếu Sữa Chữa\r\nTên Khách Hàng\r\nBiển Số";
                //cbPhieuSuaChua.DropDownWidth = 300;

                cbNhanVien.DataSource = BO.DanhSachNhanVienBO.DanhSachNhanVien();
                cbNhanVien.DisplayMember = "TenNguoiDung";
                cbNhanVien.ValueMember = "MaNguoiDung";
                //cbNhanVien.DropDownColumns = "MaNguoiDung,TenNguoiDung";
                //cbNhanVien.DropDownColumnsHeaders = "Mã Nhân Viên\r\nTên Nhân Viên\r\n";
                //cbNhanVien.DropDownWidth = 200;

                dpNgayNop.CustomFormat = "dd/MM/yyyy";
                dpNgayNop.Format = DateTimePickerFormat.Custom;

                txtSoTienNop.Text = "0";
                SUACHUA ps = BO.PhieuThuTienBO.PhieuSuaChuaTheoMa(cbPhieuSuaChua.SelectedValue.ToString());
                BAOGIA bg = BO.PhieuThuTienBO.PhieuBaoGiaTheoMa(ps.MaBaoGia);
                txtTongTien.Text = Convert.ToInt32(bg.TongTien).ToString();
                txtConLai.Text = Convert.ToInt32(ps.ConLai).ToString();

                Table<PHIEUTHU> danhsachphieuthu = BO.PhieuThuTienBO.DanhSachPhieuThu();
                var dsphieubaogia = from ds1 in danhsachphieuthu
                                    select new { ds1.MaPhieuThuTien, ds1.MaPhieuSuaChua, ds1.NguoiNopTien, ds1.DienThoai, ds1.TienThu, ds1.TienConLai };
                gvPhieuThuTien.DataSource = dsphieubaogia;
               
                
            }
            catch
            {
            }
        }

        private void cbPhieuSuaChua_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (cbPhieuSuaChua.SelectedValue.ToString().Contains('{') == false)
                {
                    SUACHUA ps = BO.PhieuThuTienBO.PhieuSuaChuaTheoMa(cbPhieuSuaChua.SelectedValue.ToString());
                    BAOGIA bg = BO.PhieuThuTienBO.PhieuBaoGiaTheoMa(ps.MaBaoGia);
                    txtTongTien.Text = Convert.ToInt32(bg.TongTien).ToString();
                    txtConLai.Text = Convert.ToInt32(ps.ConLai).ToString();
                }
            }
            catch
            {

            }
        }

        private void txtSoTienNop_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }
        void LoadPhieuThuTien(string phieuthutien)
        {
            PHIEUTHU phieuthu = BO.PhieuThuTienBO.DanhSachPhieuThu(phieuthutien);
            txtPhieuThuTien.Text = phieuthu.MaPhieuThuTien;
            cbPhieuSuaChua.Text = phieuthu.MaPhieuSuaChua;
            cbNhanVien.Text=phieuthu.NGUOIDUNG.TenNguoiDung;
            txtConLai.Text=Convert.ToInt32(phieuthu.TienConLai).ToString();
            txtSoTienNop.Text=Convert.ToInt32(phieuthu.TienThu).ToString();
            txtNguoiNopTien.Text = phieuthu.NguoiNopTien;
            txtDiaChi.Text = phieuthu.DiaChi;
            dpNgayNop.Value = phieuthu.NgayThuTien;
            txtDienThoai.Text = phieuthu.DienThoai;
        }
        private void gvPhieuThuTien_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int index = e.RowIndex;

                phieuthutien = gvPhieuThuTien.Rows[index].Cells["MaPhieuThuTien"].Value.ToString().Trim();
                LoadPhieuThuTien(phieuthutien);
                cbPhieuSuaChua.Enabled = false;
                cbNhanVien.Enabled = false;
                txtNguoiNopTien.ReadOnly = true;
                txtDienThoai.ReadOnly = true;
                txtDiaChi.ReadOnly = true;
                txtSoTienNop.ReadOnly = true;
               
               
            }
            catch
            {

            }
        }

        private void labelX7_Click(object sender, EventArgs e)
        {

        }

        private void btnThem_Click(object sender, EventArgs e)
        {

            kiemtrathem = true;
            cbNhanVien.Enabled = true;
            txtNguoiNopTien.ReadOnly = false;
            txtDienThoai.ReadOnly = false;
            txtDiaChi.ReadOnly = false;
            txtSoTienNop.ReadOnly = false;
            btnLuu.Enabled = true;
            btnThem.Enabled = false;
            btnSua.Enabled = false;
            cbPhieuSuaChua.Enabled = true;
            dpNgayNop.Enabled = true;
          
            if (BO.PhieuThuTienBO.MaPhieuThuCuoiCung() != null)
                txtPhieuThuTien.Text = NextID(BO.PhieuThuTienBO.MaPhieuThuCuoiCung().MaPhieuThuTien, "PT");
            else txtPhieuThuTien.Text = "PT0001";
            
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            inmaphieuthu = txtPhieuThuTien.Text.Trim();
            InPhieuThuTienForm inphieuthutien = new InPhieuThuTienForm();
            inphieuthutien.Show();
        }

        private void panelEx2_Click(object sender, EventArgs e)
        {

        }

        private void txtSoTienNop_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (txtSoTienNop.Text.Trim() != "")
                {

                    SUACHUA ps = BO.PhieuThuTienBO.PhieuSuaChuaTheoMa(cbPhieuSuaChua.SelectedValue.ToString());
                    int conlai = Convert.ToInt32(ps.ConLai);
                    txtConLai.Text = (conlai - Convert.ToInt32(txtSoTienNop.Text.Trim())).ToString();
                }
            }
            catch
            {
            }
        }
    }
}
